package com.xnx3.banmahuishou.superadmin.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xnx3.BaseVO;
import com.xnx3.DateUtil;
import com.xnx3.banmahuishou.core.entity.LuckDrawPrize;
import com.xnx3.j2ee.entity.System;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.util.Page;
import com.xnx3.j2ee.util.Sql;

/**
 * 奖品设置
 * @author zxy
 */
@Controller(value="BanmahuishouSuperadminPrizeSettingController")
@RequestMapping("/superadmin/prizesetting/")
public class PrizeSettingController extends BaseController {
	@Resource
	private SqlService sqlService;
	
	/**
	 * 奖品列表
	 */
	@RequestMapping("list${url.suffix}")
	public String list(HttpServletRequest request,Model model){
		Sql sql = new Sql(request);
		sql.setSearchColumn(new String[]{"prize_name","type="});
		int count = sqlService.count("laji_luck_draw_prize", sql.getWhere());
		Page page = new Page(count, 15, request);
		sql.setSelectFromAndPage("SELECT * FROM laji_luck_draw_prize", page);
		sql.setDefaultOrderBy("laji_luck_draw_prize.id DESC");
		List<LuckDrawPrize> list = sqlService.findBySql(sql, LuckDrawPrize.class);
		ActionLogUtil.insert(request, "奖品管理", "第"+page.getCurrentPageNumber()+"页");
		//计算总的中奖概率
		int allOdds = 0;
		for (int i = 0; i < list.size(); i++) {
			LuckDrawPrize draw = list.get(i);
			allOdds = allOdds + draw.getPrizeProbability();
		}

		model.addAttribute("page", page);
		model.addAttribute("list", list);
		model.addAttribute("allOdds",allOdds);
		return "/superadmin/prizesetting/list";
	}
	
	
	/**
	 * 新增/修改
	 * @param id 要修改的奖品id 。 如果是新增，这里传入0或者不传
	 */
	@RequestMapping("edit${url.suffix}")
	public String edit(
			@RequestParam(value = "id", required = false, defaultValue="0") int id,
			Model model, HttpServletRequest request){
		LuckDrawPrize entity;
		if(id < 1){
			//新增
			entity = new LuckDrawPrize();
			//日志
			ActionLogUtil.insert(request, "进入奖品新增页面");
		}else{
			//修改
			entity = sqlService.findById(LuckDrawPrize.class, id);
			if(entity == null){
				return error(model, "要修改的奖品不存在");
			}
			//日志
			ActionLogUtil.insert(request, "进入修改奖品页面", entity.toString());
		}

		//编辑页面
		model.addAttribute("entity", entity);
		return "/superadmin/prizesetting/edit";
	}

	/**
	 * 新增、修改的进行保存
	 */
	@RequestMapping(value = "save.json", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO variableSave(LuckDrawPrize inputEntity, HttpServletRequest request){
		LuckDrawPrize entity = null;
		if(!StringUtils.hasLength(inputEntity.getPrizeName())) {
			return error("请输入奖品名称");
		}
		if(inputEntity.getPrizeNum() == null) {
			return error("请输入奖励额度");
		}
		if(inputEntity.getCount() == null) {
			return error("请输入奖品个数");
		}
		if(!StringUtils.hasLength(inputEntity.getPrizelevel())) {
			return error("请输入奖品等级");
		}
		if(inputEntity.getPrizeProbability() == null) {
			return error("请输入中奖概率");
		}
		if(inputEntity.getId() == null || inputEntity.getId() < 1){
			//新增
			entity = new LuckDrawPrize();

			//因为是新增，所以要加上新增时间
			entity.setAddTime(DateUtil.timeForUnix10());	
		}else{
			//修改
			entity = sqlService.findById(LuckDrawPrize.class, inputEntity.getId());
			if(entity == null){
				return error("要修改的目标不存在");
			}
		}
		entity.setCount(inputEntity.getCount());
		entity.setPrizelevel(inputEntity.getPrizelevel());
		entity.setPrizeName(inputEntity.getPrizeName());
		entity.setPrizeNum(inputEntity.getPrizeNum());
		entity.setType(inputEntity.getType());
		entity.setPrizeSort(inputEntity.getPrizeSort());;
		if (inputEntity.getPrizeProbability() > 100) {
			return error("请重新设置概率");
		}else {
			entity.setPrizeProbability(inputEntity.getPrizeProbability());
		}
		
		entity.setPrizeProbability(inputEntity.getPrizeProbability());
		sqlService.save(entity);
		//插入日志
		ActionLogUtil.insertUpdateDatabase(request, entity.getId(), "保存奖品信息", entity.toString());
		return success();
	}

	/**
	 * 删除奖品信息
	 * @param id 奖品id
	 * @author zxy
	 */
	@RequestMapping(value="delete.json", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO delete(@RequestParam(value = "id", required = true, defaultValue="0") int id, HttpServletRequest request){
		LuckDrawPrize entity = sqlService.findById(LuckDrawPrize.class, id);
		if(entity == null){
			return error("要删除的目标不存在");
		}
		sqlService.delete(entity);
		//插入日志
		ActionLogUtil.insertUpdateDatabase(request, entity.getId(), "删除奖品", entity.toString());
		return success();
	}
	
	
	
	/**
	 * 进入设置金额页面
	 */
	@RequestMapping("set${url.suffix}")
	public String set(HttpServletRequest request,Model model){
		//设置参数的名字
		String name = "LOTTERY_AMOUNT_SETTING";
		//取出这条记录
		System system = sqlService.findAloneByProperty(System.class, "name", name);
		model.addAttribute("entity", system);
		//日志
		ActionLogUtil.insert(request, "进入金额设置页面");
		return "/superadmin/prizesetting/set";
	}

	/**
	 * 保存设置金额
	 * @param id 要修改的金额参数的id  对应system.id
	 * @param value 修改的金额 单位 元
	 * @author zxy
	 */
	@RequestMapping(value="setMoney.json", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO setMoney(HttpServletRequest request,
			@RequestParam(value = "id", required = true, defaultValue="0") int id,
			@RequestParam(value = "value", required = true, defaultValue="0") int value){
		//取金额设置参数的记录
		System system = sqlService.findById(System.class, id);
		if (system == null) {
			return error("要设置的目标不存在");
		}
		system.setValue(Integer.toString(value));
		system.setLasttime(DateUtil.timeForUnix10());
		sqlService.save(system);
		//插入日志
		ActionLogUtil.insertUpdateDatabase(request, "保存金额");
		return success();
	}
	
}
